﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.LevelDataAccessModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace GovernmentOfficeProject.LevelDataAccess
{
    public class DAL_COM_Sms_Log
    {

        public List<Entity_COM_Sms_Log> getSmsLogList()
        {
            try
            {
                string strSQL = "select * from [COM_SMS_LOG]";
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                Entity_COM_Sms_Log smsLog = null;
                List<Entity_COM_Sms_Log> smsLogList = new List<Entity_COM_Sms_Log>();
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    smsLog = new Entity_COM_Sms_Log();
                    smsLog.SlID = Convert.ToInt32(row["SLID"]);
                    smsLog.SlSmsID = Convert.ToInt32(row["SL_SMSID"]);
                    smsLog.SlFromUserID =  Convert.ToDecimal(row["SL_FromUserID"]);
                    smsLog.SlToUserID = Convert.ToDecimal(row["SL_ToUserID"]);
                    smsLog.SlToMobilePhone = Convert.ToString(row["SL_ToMobilePhone"]);
                    smsLog.SlStatus = Convert.ToDecimal(row["SL_Status"]);
                    smsLog.Backup1 = Convert.ToString(row["SL_Backup1"]);
                    smsLog.Backup2 = Convert.ToString(row["SL_Backup2"]);
                    smsLog.Backup3 = Convert.ToString(row["SL_Backup3"]);
                    smsLog.Backup4 = Convert.ToString(row["SL_Backup4"]);
                    smsLog.Backup5 = Convert.ToString(row["SL_Backup5"]);
                    smsLogList.Add(smsLog);
                }
                return smsLogList;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_COM_Sms_Log.getSmsLogList()---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public List<Entity_COM_Sms_Log> getSmsLogListBySmsEntity(Entity_COM_Sms_Log smsLogEntity)
        {
            try
            {
                string strSQL = "select * from [COM_SMS_LOG] where 1=1";
                if (smsLogEntity.SlSmsID != -1)
                {
                    strSQL += " and SL_SMSID=" + smsLogEntity.SlSmsID;
                }
                if (smsLogEntity.SlFromUserID != -1)
                {
                    strSQL += " and SL_FromUserID=" + smsLogEntity.SlFromUserID;
                }
                if (smsLogEntity.SlToUserID != -1)
                {
                    strSQL += " and SL_ToUserID=" + smsLogEntity.SlToUserID ;
                }
                if (smsLogEntity.SlToMobilePhone !="" && smsLogEntity.SlToMobilePhone!=null)
                {
                    strSQL += " and SL_ToMobilePhone='" + smsLogEntity.SlToMobilePhone + "'";
                }
                if (smsLogEntity.SlStatus != -1)
                {
                    strSQL += " and SL_Status=" + smsLogEntity.SlStatus;
                }
                if (smsLogEntity.Backup1!="" && smsLogEntity.Backup1!=null)
                {
                    strSQL += " and SL_Backup1='" + smsLogEntity.Backup1+"'";
                }
                if (smsLogEntity.Backup2 != "" && smsLogEntity.Backup2 != null)
                {
                    strSQL += " and SL_Backup2='" + smsLogEntity.Backup2 + "'";
                }
                if (smsLogEntity.Backup3 != "" && smsLogEntity.Backup3 != null)
                {
                    strSQL += " and SL_Backup3='" + smsLogEntity.Backup3 + "'";
                }
                if (smsLogEntity.Backup4 != "" && smsLogEntity.Backup4 != null)
                {
                    strSQL += " and SL_Backup4='" + smsLogEntity.Backup4 + "'";
                }
                if (smsLogEntity.Backup5 != "" && smsLogEntity.Backup5 != null)
                {
                    strSQL += " and SL_Backup5='" + smsLogEntity.Backup5 + "'";
                }
                DataSet dst = DAL_DBGM.GetDataSet(strSQL);
                Entity_COM_Sms_Log smsLog = null;
                List<Entity_COM_Sms_Log> smsLogList = new List<Entity_COM_Sms_Log>();
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    smsLog = new Entity_COM_Sms_Log();
                    smsLog.SlID = Convert.ToInt32(row["SLID"]);
                    smsLog.SlSmsID = Convert.ToInt32(row["SL_SMSID"]);
                    smsLog.SlFromUserID = Convert.ToDecimal(row["SL_FromUserID"]);
                    smsLog.SlToUserID = Convert.ToDecimal(row["SL_ToUserID"]);
                    smsLog.SlToMobilePhone = Convert.ToString(row["SL_ToMobilePhone"]);
                    smsLog.SlStatus = Convert.ToDecimal(row["SL_Status"]);
                    smsLog.Backup1 = Convert.ToString(row["SL_Backup1"]);
                    smsLog.Backup2 = Convert.ToString(row["SL_Backup2"]);
                    smsLog.Backup3 = Convert.ToString(row["SL_Backup3"]);
                    smsLog.Backup4 = Convert.ToString(row["SL_Backup4"]);
                    smsLog.Backup5 = Convert.ToString(row["SL_Backup5"]);
                    smsLogList.Add(smsLog);
                }
                return smsLogList;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_COM_Sms_Log.getSmsLogListBySmsEntity()---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public Entity_COM_Sms_Log getSmsLogBySmsID(int smsLogID)
        {
            try
            {
                string strSQL = "select * from [COM_SMS_LOG] where SLID=@slID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@slID",smsLogID)
                };
                DataSet dst = DAL_DBGM.GetDataSet(strSQL,values);
                Entity_COM_Sms_Log smsLog = null;
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    smsLog = new Entity_COM_Sms_Log();
                    smsLog.SlID = Convert.ToInt32(row["SLID"]);
                    smsLog.SlSmsID = Convert.ToInt32(row["SL_SMSID"]);
                    smsLog.SlFromUserID = Convert.ToDecimal(row["SL_FromUserID"]);
                    smsLog.SlToUserID = Convert.ToDecimal(row["SL_ToUserID"]);
                    smsLog.SlToMobilePhone = Convert.ToString(row["SL_ToMobilePhone"]);
                    smsLog.SlStatus = Convert.ToDecimal(row["SL_Status"]);
                    smsLog.Backup1 = Convert.ToString(row["SL_Backup1"]);
                    smsLog.Backup2 = Convert.ToString(row["SL_Backup2"]);
                    smsLog.Backup3 = Convert.ToString(row["SL_Backup3"]);
                    smsLog.Backup4 = Convert.ToString(row["SL_Backup4"]);
                    smsLog.Backup5 = Convert.ToString(row["SL_Backup5"]);
                }
                return smsLog;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_COM_Sms_Log.getSmsLogBySmsID(int smsLogID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public bool addSmsLog(Entity_COM_Sms_Log smsLogEntity)
        {
            try
            {
                bool flag = false;
                bool blState = false;
                StringBuilder sqlBudIns = new StringBuilder();
                StringBuilder sqlBudValue = new StringBuilder();
                sqlBudIns.Append("insert into [COM_SMS_LOG](");
                sqlBudValue.Append(" values(");
                List<SqlParameter> sprList = new List<SqlParameter>();

                if (smsLogEntity.SlSmsID != -1)
                {
                    sqlBudIns.Append("SL_SMSID,");
                    sqlBudValue.Append("@smsID,");
                    sprList.Add(new SqlParameter("@smsID", smsLogEntity.SlSmsID));
                    blState = true;
                }

                if (smsLogEntity.SlFromUserID != -1)
                {
                    sqlBudIns.Append("SL_FromUserID,");
                    sqlBudValue.Append("@fromUserID,");
                    sprList.Add(new SqlParameter("@fromUserID", smsLogEntity.SlFromUserID));
                    blState = true;
                }
                if (smsLogEntity.SlToUserID != -1)
                {
                    sqlBudIns.Append("SL_ToUserID,");
                    sqlBudValue.Append("@toUserID,");
                    sprList.Add(new SqlParameter("@toUserID", smsLogEntity.SlToUserID));
                    blState = true;
                }
                if (smsLogEntity.SlToMobilePhone!="" && smsLogEntity.SlToMobilePhone != null)
                {
                    sqlBudIns.Append("SL_ToMobilePhone,");
                    sqlBudValue.Append("@toMobilePhone,");
                    sprList.Add(new SqlParameter("@toMobilePhone", smsLogEntity.SlToMobilePhone));
                    blState = true;
                }
                if (smsLogEntity.SlStatus != -1)
                {
                    sqlBudIns.Append("SL_Status,");
                    sqlBudValue.Append("@status,");
                    sprList.Add(new SqlParameter("@status", smsLogEntity.SlStatus));
                    blState = true;
                }
                if (smsLogEntity.Backup1!="" && smsLogEntity.Backup1 != null)
                {
                    sqlBudIns.Append("SL_Backup1,");
                    sqlBudValue.Append("@backup1,");
                    sprList.Add(new SqlParameter("@backup1", smsLogEntity.Backup1));
                    blState = true;
                }
                if (smsLogEntity.Backup2 != "" && smsLogEntity.Backup2 != null)
                {
                    sqlBudIns.Append("SL_Backup2,");
                    sqlBudValue.Append("@backup2,");
                    sprList.Add(new SqlParameter("@backup2", smsLogEntity.Backup2));
                    blState = true;
                }
                if (smsLogEntity.Backup3 != "" && smsLogEntity.Backup3 != null)
                {
                    sqlBudIns.Append("SL_Backup3,");
                    sqlBudValue.Append("@backup3,");
                    sprList.Add(new SqlParameter("@backup3", smsLogEntity.Backup3));
                    blState = true;
                }
                if (smsLogEntity.Backup4 != "" && smsLogEntity.Backup4 != null)
                {
                    sqlBudIns.Append("SL_Backup4,");
                    sqlBudValue.Append("@backup4,");
                    sprList.Add(new SqlParameter("@backup4", smsLogEntity.Backup4));
                    blState = true;
                }
                if (smsLogEntity.Backup5 != "" && smsLogEntity.Backup5 != null)
                {
                    sqlBudIns.Append("SL_Backup5,");
                    sqlBudValue.Append("@backup5,");
                    sprList.Add(new SqlParameter("@backup5", smsLogEntity.Backup5));
                    blState = true;
                }

                if (blState)
                {
                    string strSQL = sqlBudIns.ToString().Substring(0, sqlBudIns.Length - 1) + ")" + sqlBudValue.ToString().Substring(0, sqlBudValue.Length - 1) + ")";
                    SqlParameter[] values = sprList.ToArray();
                    AppLog.Write("DAL-增加SMS的SQL语句: " + strSQL, AppLog.LogMessageType.Debug);
                    flag = DAL_DBGM.InsertInfo(strSQL, values);
                }
                else
                {
                    //添加用户失败:没有任何数据.
                    AppLog.Write("添加SMS失败:没有任何数据.", AppLog.LogMessageType.Info);
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_COM_Sms_Log.addSmsLog(Entity_COM_Sms_Log smsLogEntity)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        public bool deleteSmsLogBySmsLogID(int smsLogID)
        {
            try
            {
                bool flag = false;
                string strSQL = "delete from [COM_SMS_LOG] where SLID=@slID";
                SqlParameter[] values = new SqlParameter[]{
                    new SqlParameter("@slID",smsLogID)
                };
                flag = DAL_DBGM.DeleteInfo(strSQL, values);
                return flag;
            }
            catch (System.Exception ex)
            {
                AppLog.Write("---DAL_COM_Sms_Log.tSmsEntityBySmsID(int smsID)---", SysTools.AppLog.LogMessageType.Error);
                throw ex;
            }
        }

        //updateSmsLogBySmsLogIDAndSmsEntity(0修改方法暂不需要.
    }
}
